﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
		Subject	:	 งานทุกอย่างที่เกี่ยวกับรายวิชา
		เพิ่ม ลบ แก้ไข
\												/


*/

$(function()
{
	getsubdata();
});

function getsubdata()
{
	$('#workarea').html('<tr><th width="30">Id</th><th width="200">Deparment Name</th><th width="200">Group Name</th><th width="30">Number</th><th width="400">Name</th><th width="30">Credit</th><th width="400">Teacher</th><th width="400">Subject Req</th><th width="50">Edit</th><th width="50">Delete</th></tr>');
	$.getJSON('class/_reqsub.php',function(data)
	{
		$.each(data,function(key,val){
		$('#workarea').html($('#workarea').html()+'<tr id="data'+val['id']+'"><td id="id'+val['id']+'">'+ val['id']+'</td><td id="dname'+val['id']+'"><input type="hidden" id="did'+val['id']+'" value="'+val['did']+'">'+ val['dName']+'</td><td id="gname'+val['id']+'"><input type="hidden" id="gid'+val['id']+'" value="'+val['gid']+'">'+ val['gName']+'</td><td id="number'+val['id']+'">'+ val['number']+'</td><td id="name'+val['id']+'">'+val['name']+'</td><td id="credit'+val['id']+'">'+val['credit']+ '</td><td id="tname'+val['id']+'"><input type="hidden" id="tid'+val['id']+'" value="'+val['tid']+'">'+val['tName']+ '</td><td id="sname'+val['id']+'"><input type="hidden" id="sid'+val['id']+'" value="'+val['sid']+'">'+val['sName']+ '</td><td id="edit'+val['id']+'"><li class="ui-state-default ui-corner-all" id="editbutton'+val['id']+'" title="Edit" onClick="editsubdata('+val['id']+')"><span class="ui-icon ui-icon-pencil" ></span></li></td><td><li class="ui-state-default ui-corner-all" id="delbutton'+val['id']+'" title="Delete" onClick="subdelcon('+val['id']+');"><span class="ui-icon ui-icon-trash"></span></li></td></tr>');
		//$('#id'+val['id']).button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#dname'+val['id']).button().css({'cursor': 'default','width': '200','height': '20'});
		//$('#gname'+val['id']).button().css({'cursor': 'default','width': '200','height': '20'});
		//$('#number'+val['id']).button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#credit'+val['id']).button().css({'cursor': 'default','width': '400','height': '20'});
		//$('#name'+val['id']).button().css({'cursor': 'default','width': '400','height': '20'});
		//$('#tname'+val['id']).button().css({'cursor': 'default','width': '400','height': '20'});
		//$('#sname'+val['id']).button().css({'cursor': 'default','width': '400','height': '20'});
		$('#editbutton'+val['id']).button().css({'cursor': 'pointer'});
		$('#delbutton'+val['id']).button().css({'cursor': 'pointer'});
		});
		$('#workarea').html($('#workarea').html()+'<tr id="dataadd"><td id="idadd">ADD</td><td id="selectdep"><select id="selectdepadd"></select></td><td id="selectgop"><select id="selectgopadd"></select></td><td id="numberadd"><input type="text"  value="" id="txtnumberadd" size="7"></td><td id="nameadd"><input type="text"  value="" id="txtnameadd" size="50"></td><td id="creditadd"><input type="number" id="txtcreditadd" min="1" max="9"></td><td id="selecttea"><select id="selectteaadd"></select></td><td id="selectsub"><select id="selectsubadd"></select></td><td colspan="2"><li class="ui-state-default ui-corner-all" id="addbutton" title="Add" onClick="subadd();"><span class="ui-icon ui-icon-check"></span></li></td></tr>');
		//$('#idadd').button().css({'width': '30','height': '20'});
		depselect('#selectdepadd');
		//$('#selectdep').button().css({'cursor': 'default','width': '200','height': '20'});
		$('#selectdepadd').change(function () {
			$('#selectdepadd option:selected').each(function () {
				gopselect('#selectgopadd',$('#selectdepadd').val());
			});
		}).trigger('change');
		//$('#selectgop').button().css({'cursor': 'default','width': '200','height': '20'});
		teaselect('#selectteaadd');
		//$('#selecttea').button().css({'cursor': 'default','width': '200','height': '20'});
		subselect('#selectsubadd');
		//$('#selectsub').button().css({'cursor': 'default','width': '200','height': '20'});
		//$('#numberadd').button().css({'cursor': 'default','width': '30','height': '20'});
		//$('#nameadd').button().css({'cursor': 'default','width': '400','height': '20'});
		//$('#creditadd').button().css({'cursor': 'default','width': '400','height': '20'});
		$('#addbutton').button().css({'cursor': 'pointer','width': '100'});
	});
}

function depselect(where,id)
{
		$(where).html($(where).html()+'<option value=""></option>');
		$.getJSON('class/_reqdep.php',function(data)
		{
			$.each(data,function(key,val){
			if(val['id'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'" selected="selected">'+val['number']+' - '+val['name']+'</option>');
				} else 
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
				}
			});
		});
}

function subselect(where,id)
{
		$(where).html($(where).html()+'<option value=""></option>');
		if(id == 0)
		{
			$(where).html($(where).html()+'<option value="0" selected="selected"> NONE </option>');
		}else
		{
			$(where).html($(where).html()+'<option value="0"> NONE </option>');
		}
		$.getJSON('class/_reqsub.php',function(data)
		{
			$.each(data,function(key,val){
				if(val['id'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'" selected="selected">'+val['number']+' - '+val['name']+'</option>');
				} else
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
				}
			});
		});
}

function teaselect(where,id)
{
		$(where).html($(where).html()+'<option value=""></option>');
		$.getJSON('class/_reqtea.php',function(data)
		{
			$.each(data,function(key,val){
				if(val['id'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'" selected="selected">'+val['name']+'</option>');
				} else 
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['name']+'</option>');
				}
			});
		});
}

function gopselect(where,id,gid)
{
		$(where).html($(where).html()+'<option value=""></option>');
		$.getJSON('class/_reqgop.php',function(data)
		{
			$(where).html('');
			$.each(data,function(key,val){
				if(val['did'] == id)
				{
					if(val['id'] == gid)
					{
						$(where).html($(where).html()+'<option value="'+val['id']+'" selected="selected">'+val['number']+' - '+val['name']+'</option>');
					} else 
					{
						$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
					}
				}
			});
		})
}

function subadd()
{
	if($('#txtnumberadd').val() == ''&&$('#txtnameadd').val() == '')
	{
		$('#dialog').html('Plase enter Number and Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnumberadd').val() == '')
	{
		$('#dialog').html('Plase enter Number');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnameadd').val() == '')
	{
		$('#dialog').html('Plase enter Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtcreditadd').val() == '')
	{
		$('#dialog').html('Plase enter Credit');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectdepadd').val() == '')
	{
		$('#dialog').html('Plase choise Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectgopadd').val() == '')
	{
		$('#dialog').html('Plase choise Group');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectsubadd').val() == '')
	{
		$('#dialog').html('Plase choise Subject Reqment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectteaadd').val() == '')
	{
		$('#dialog').html('Plase choise Teacher');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqsubinsert.php',{'gid':$('#selectgopadd').val(),'tid':$('#selectteaadd').val(),'sid':$('#selectsubadd').val(),'number':$('#txtnumberadd').val(),'name':$('#txtnameadd').val(),'credit':$('#txtcreditadd').val()},function(data)
		{
			$('#dialog').html('Insert Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			$('#txtnumberadd').val('');
			$('#txtnameadd').val('');
			getsubdata()
		});
	}	
}

function editsubdata(id)
{
	var did = $('#did'+id).val();
	$('#dname'+id).html('<select id="selectdep'+id+'"></select>');
	depselect('#selectdep'+id,did);
	var gid = $('#gid'+id).val();
	$('#gname'+id).html('<select id="selectgop'+id+'"></select>');
	gopselect('#selectgop'+id,did,gid);
	$('#selectdep'+id).change(function () {
		$('#selectdep'+id+' option:selected').each(function () {
			gopselect('#selectgop'+id,$('#selectdep'+id).val(),gid);
		});
	}).trigger('change');
	var tid = $('#tid'+id).val();
	$('#tname'+id).html('<select id="selecttea'+id+'"></select>');
	teaselect('#selecttea'+id,tid);
	var sid = $('#sid'+id).val();
	$('#sname'+id).html('<select id="selectsub'+id+'"></select>');
	subselect('#selectsub'+id,sid);
	$('#number'+id).html('<input type="text"  value="'+$('#number'+id).text()+'" id="txtnumber'+id+'" size="7">');
	$('#name'+id).html('<input type="text"  value="'+$('#name'+id).text()+'" id="txtname'+id+'" size="50">');
	$('#credit'+id).html('<input type="number"  value="'+$('#credit'+id).text()+'" id="txtcredit'+id+'" min="1" max="9">');
	$('#edit'+id).html('<li class="ui-state-default ui-corner-all" title="Enter" onClick="subupdate('+id+');" id="enterbutton'+id+'"><span class="ui-icon ui-icon-check"></span></li>');
	$('#enterbutton'+id).button().css({'cursor': 'pointer','width': '50'});
}

function subupdate(id)
{
	if($('#txtnumber'+id).val() == ''&&$('#txtname'+id).val() == '')
	{
		$('#dialog').html('Plase enter Number and Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtnumber'+id).val() == '')
	{
		$('#dialog').html('Plase enter Number');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtcredit'+id).val() == '')
	{
		$('#dialog').html('Plase enter Credit');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#txtname'+id).val() == '')
	{
		$('#dialog').html('Plase enter Name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectdep'+id).val() == '')
	{
		$('#dialog').html('Plase choise Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectgop'+id).val() == '')
	{
		$('#dialog').html('Plase choise Group');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selecttea'+id).val() == '')
	{
		$('#dialog').html('Plase choise Teacher');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#selectsub'+id).val() == '')
	{
		$('#dialog').html('Plase choise Subject');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqsubupdate.php',{'id':id,'gid':$('#selectgop'+id).val(),'tid':$('#selecttea'+id).val(),'sid':$('#selectsub'+id).val(),'number':$('#txtnumber'+id).val(),'name':$('#txtname'+id).val(),'credit':$('#txtcredit'+id).val()},function(data)
		{
			$('#dname'+id).html($('#selectdep'+id+' option:selected').text());
			$('#gname'+id).html($('#selectgop'+id+' option:selected').text());
			$('#tname'+id).html($('#selecttea'+id+' option:selected').text());
			$('#sname'+id).html($('#selectsub'+id+' option:selected').text());
			$('#number'+id).html($('#txtnumber'+id).val());
			$('#credit'+id).html($('#txtcredit'+id).val());
			$('#name'+id).html($('#txtname'+id).val());
			$('#edit'+id).html('<li class="ui-state-default ui-corner-all" title="Edit" onClick="editsubdata('+id+');" id="editbutton'+id+'"><span class="ui-icon ui-icon-pencil"></span></li>');
			$('#editbutton'+id).button().css({'cursor': 'pointer','width': '50'});
			$('#dialog').html('Edit Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
		});
	}
}

function subdelcon(id)
{
	$('#delcon').html('You want to Delete this?');
		$('#delcon').dialog({ buttons: [{text: 'Delete',click: function()
		{ 
			subdeldata(id);
			$( this ).dialog('close'); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
		$('#delcon').dialog({'title':'Confirm Delete'});
		$('#delcon').dialog('option','resizable',false);
}

function subdeldata(id)
{
	$.get('class/_reqsubdel.php',{'id':id},function(data)
	{
		$('#dialog').html('Delete Success');
		$('#dialog').dialog({'title':'Success'});
		$('#dialog').dialog('option','resizable',false);
		$('#data'+id).hide();
	});
}